<?php

namespace app\common\model;

use think\Exception;
use think\Model;


class Order extends Model
{

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_time_text'
    ];

    const PAY_PAID_WAIT = 0; // 未支付
    const PAY_METHOD_WECHAT = 1; // 微信
    const PAY_METHOD_ALI = 2; // 阿里云

    const IS_PAID_WAIT = 1; // 未支付
    const IS_PAID_PAY = 2; // 已支付

    const IS_CANCEL_NOT = 1; // 未取消
    const IS_CANCEL_YES = 2; // 已取消


    public static function getPayMethodList(){
        return [
            self::PAY_PAID_WAIT         => '未支付',
            self::PAY_METHOD_ALI        => '支付宝',
            self::PAY_METHOD_WECHAT     => '微信',
        ];
    }

    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id')->setEagerlyType(0);
    }

    public function store()
    {
        return $this->belongsTo('Store', 'store_id', 'id', [], 'left')->setEagerlyType(0);
    }

    // 过期时间
    public static function getExpirationTime($mine = 5){
        return time() + ($mine * 60);
    }

    public static function create_order_sn()
    {
        $order_sn = date('YmdHis').mt_rand(100000, 999999);

        if(self::where('order_sn', $order_sn)->count()){
            $order_sn = self::create_order_sn();
        }
        if(OrderThird::where('third_order_sn', $order_sn)->count()){
            $order_sn = self::create_order_sn();
        }
        return $order_sn;
    }

    // 支付成功
    public static function paySuccess($order, $params = []){
//        $order = self::query()->where('order_sn', $order_sn)->first();
//        if(empty($order)){
//            throw new Exception("订单不存在");
//        }
        $time = time();
        $saveData = [
            'is_paid'  => self::IS_PAID_PAY,
            'pay_time' => $time,
        ];
        if(isset($params['transaction_id'])){
            $saveData['transaction_id'] = $params['transaction_id'];
        }

        self::where('id', $order->id)->update($saveData);

        $user_count = self::where('store_id', $order['store_id'])->where('is_paid', self::IS_PAID_PAY)->where('is_cancel', self::IS_CANCEL_NOT)->group('user_id')->count();

        $store = Store::where('id', $order['store_id'])->find();
        $store->ticket += 1;
        $store->ticket_money += $order['money'];
        $store->ticket_user = $user_count;
        $store->save();

        return true;
    }

    // 取消订单
    public static function cancel_order($order, $remark = ''){
        if($order['is_cancel'] == Order::IS_CANCEL_YES){
            throw new Exception('该订单已取消');
        }
        $saveData = ['is_cancel'=>Order::IS_CANCEL_YES, 'cancel_time' => time()];
        if($remark){
            $saveData['cancel_remark'] = $remark;
        }
        $result = $order->save($saveData);

        if($result){
            return true;
        }
        throw new Exception('订单取消失败');
    }
}
